package com.itsv.xhyy;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Iterator;
import java.util.List;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.app.Dialog;
import android.content.Context;
import android.content.Intent;
import android.graphics.PixelFormat;
import android.net.Uri;
import android.os.AsyncTask;
import android.os.Bundle;
import android.os.Handler;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.View.OnTouchListener;
import android.view.WindowManager;
import android.view.WindowManager.LayoutParams;
import android.view.inputmethod.InputMethodManager;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ExpandableListView.OnChildClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ExpandableListView;
import android.widget.ExpandableListView.OnGroupClickListener;
import android.widget.ImageButton;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.PopupWindow;
import android.widget.ProgressBar;
import android.widget.TextView;
import android.widget.Toast;

import com.itsv.contacts.view.RightCharacterListView;
import com.itsv.contacts.view.RightCharacterListView.OnTouchingLetterChangedListener;
import com.itsv.cyzx.adapter.ContactsBaseAdapter;
import com.itsv.cyzx.adapter.ContactsJiebieBaseAdapter;
import com.itsv.cyzx.adapter.ContactsweiyuanhuiBaseAdapter;
//import com.itsv.cyzx.adapter.ContactsJiebieBaseAdapter2;
import com.itsv.cyzx.bean.ContactsBanBen;
import com.itsv.cyzx.bean.ContactsBean;
import com.itsv.cyzx.db.DBHelper;
import com.itsv.cyzx.db.DBHelperCon;
import com.itsv.cyzx.util.CnToSpellUtils;
import com.itsv.cyzx.util.Comparent;
import com.itsv.cyzx.util.DataUtil;
import com.itsv.cyzx.util.JsonUtil;
import com.itsv.cyzx.util.ListTest;
import com.itsv.cyzx.util.StringUtil;

public class ContactsActivity extends Activity implements OnClickListener,
		OnItemClickListener, OnChildClickListener, OnGroupClickListener {
	protected static final int FINISHED_NET = 0;
	protected static final int FINISHED_LOCAL = 1;
	private static final int FINISHED_JIEBIE = 2;
	private static final int FINISHED_TXL = 3;
	String[] b = { "#", "A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K",
			"L", "M", "N", "O", "P", "Q", "R", "S", "T", "U", "V", "W", "X",
			"Y", "Z", "" };
	String[] c = { "" };
	private String searchType = "name";
	private ListView mContactsListView;
	private ExpandableListView mContactsListView2;
	public static TextView mTxtOverLay;
	private List<List<ContactsBean>> mData = new ArrayList<List<ContactsBean>>();
	private String[] mGroupArrays = new String[] {};
	private ArrayList<ContactsBean> mJieBieListData;
	private ArrayList<ContactsBean> mnametData;
	private ArrayList<ContactsBean> mAllCListData;
	private ArrayList<ContactsBean> mjiebieData;

	// 右上角切换按钮 界别和全部 切换标识 默认显示界别
	private boolean isJiebie = false;
	private boolean isname = false;
	private boolean iszhuanweihui = true;
	private DBHelperCon dbHelper;
	private String oldBanben = "0", newBanben;
	private ArrayList<ContactsBean> contactsBeans = new ArrayList<ContactsBean>();
	private ContactsBaseAdapter contactsBaseAdapter;
	private ContactsweiyuanhuiBaseAdapter weiyuanBaseAdapter;
	private ContactsJiebieBaseAdapter jiebieBaseAdapter = null;
	private ProgressBar mProgressBar;
	int expandFlag = -1;// 控制列表的展开

	private String number = null;
	private PopupWindow mPopupWindow;
	private WindowManager mWindowManager;
	private DBHelper mDbHelper;
	private RightCharacterListView mRightCharacterListView;
	private ImageButton mChangeBtn;
	private EditText mSearchEditText;
	private ImageButton mCancelBtn, refButton;
	private boolean isjiebie = false;
	private ContactsBean bean;
	private Boolean chat = false;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.layout_contacts);
		mDbHelper = new DBHelper(this);
		dbHelper = new DBHelperCon(this);
		addOverLay();
		addRightView();
		init();
	}

	private void init() {
		mjiebieData = new ArrayList<ContactsBean>();
		Intent intent = getIntent();
		chat = intent.getBooleanExtra("chat", false);
		mContactsListView = (ListView) findViewById(R.id.contactsListView);
		mContactsListView2 = (ExpandableListView) findViewById(R.id.contactsListView2);
		mContactsListView.setOnItemClickListener(ContactsActivity.this);
		mContactsListView2.setOnChildClickListener(this);
		mContactsListView2.setOnGroupClickListener(this);
		mContactsListView2.setOnItemClickListener(ContactsActivity.this);
		initHeaderView();
		oldBanben = dbHelper.getContactsBanBen();
		Button backBtn = (Button) findViewById(R.id.btn_goback);
		mChangeBtn = (ImageButton) findViewById(R.id.btn_change);
		refButton = (ImageButton) findViewById(R.id.btn_ref);
		refButton.setOnClickListener(this);
		backBtn.setOnClickListener(this);
		mChangeBtn.setOnClickListener(this);
		mContactsListView2.setVisibility(View.GONE);
		mContactsListView.setVisibility(View.VISIBLE);
		mProgressBar = (ProgressBar) findViewById(R.id.progressBar1);
		ll.setVisibility(View.VISIBLE);
		// new GetBanBenTask().execute();
		new GetDBDataTask().execute();

	}

	private void initHeaderView() {
		ll = (LinearLayout) findViewById(R.id.layout_search);
		mSearchEditText = (EditText) findViewById(R.id.searchEdit);
		mSearchEditText.setOnTouchListener(new OnTouchListener() {
			@Override
			public boolean onTouch(View v, MotionEvent event) {
				if (searchType != null && searchType.equals("jiebie")) {
					if (mRightCharacterListView.getVisibility() == View.VISIBLE) {
						mRightCharacterListView.setVisibility(View.GONE);
					}
				}
				if (mCancelBtn.getVisibility() == View.GONE) {
					mCancelBtn.setVisibility(View.VISIBLE);
					mSearchEditText.setFocusable(true);
					mSearchEditText.setFocusableInTouchMode(true);
					mSearchEditText.requestFocus();
				}
				return false;
			}
		});
		mCancelBtn = (ImageButton) findViewById(R.id.cancelButton);
		// 默认隐藏
		mCancelBtn.setVisibility(View.GONE);
		mCancelBtn.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				closeInputMethod();
				mCancelBtn.setVisibility(View.GONE);
				mSearchEditText.getText().clear();
				mSearchEditText.setFocusable(false);
				mnametData = mAllCListData;
				if (searchType != null && searchType.equals("name")) {
					mRightCharacterListView.setVisibility(View.VISIBLE);
					mContactsListView.setAdapter(new ContactsBaseAdapter(
							ContactsActivity.this, mnametData,
							mContactsListView));
				} else if (searchType != null && searchType.equals("jiebie")) {
					mGroupArrays = ListTest.getJieBie(mjiebieData);
					mData = ListTest.getLists(mGroupArrays, mjiebieData);
					jiebieBaseAdapter = new ContactsJiebieBaseAdapter(
							ContactsActivity.this, mData, mGroupArrays,
							mContactsListView2);
					mContactsListView2.setAdapter(jiebieBaseAdapter);

				}
			}
		});
		mSearchEditText.addTextChangedListener(new TextWatcher() {
			@Override
			public void onTextChanged(CharSequence s, int start, int before,
					int count) {
				final ArrayList<ContactsBean> tempList = new ArrayList<ContactsBean>();
				// 将输入内容转换为拼音
				String string = mSearchEditText.getText().toString();
				if (mnametData != null && mnametData.size() != 0) {
					for (int i = 0; i < mnametData.size(); i++) {
						ContactsBean beanEntity = mnametData.get(i);
						// 将用户名转换为拼音
						String uname = beanEntity.getName();
						// 包含添加列表中
						if (string != null && !string.trim().equals("")
								&& uname.contains(string)) {
							tempList.add(beanEntity);
						}
					}
					if (string != null && !string.trim().equals("")) {
						mnametData = tempList;
					} else {
						mnametData = mAllCListData;
					}
					if (searchType != null && searchType.equals("name")) { // 拼音
						mContactsListView.setAdapter(new ContactsBaseAdapter(
								ContactsActivity.this, mnametData,
								mContactsListView));
					} else if (searchType != null
							&& searchType.equals("jiebie")) {
						mGroupArrays = ListTest.getJieBie(mjiebieData);
						mData = ListTest.getLists(mGroupArrays, mjiebieData);
						jiebieBaseAdapter = new ContactsJiebieBaseAdapter(
								ContactsActivity.this, mData, mGroupArrays,
								mContactsListView2);
						mContactsListView2.setAdapter(jiebieBaseAdapter);
					}
				}
			}

			@Override
			public void beforeTextChanged(CharSequence s, int start, int count,
					int after) {

			}

			@Override
			public void afterTextChanged(Editable s) {

			}
		});
	}

	/**
	 * 姓名数据
	 */
	private void addNameListData() {
		ll.setVisibility(View.VISIBLE);
		findViewById(R.id.rightCharacterListView).setVisibility(View.VISIBLE);
		if (mContactsListView == null) {
			new Thread() {
				public void run() {
					new GetBanBenTask().execute();
				}
			}.start();
		} else {
			findViewById(R.id.progressBar1).setVisibility(View.INVISIBLE);
		}
	}

	/**
	 * 界别数据
	 */
	private void addJiebieListData() {
		ll.setVisibility(View.GONE);
		new Thread() {
			public void run() {
				findViewById(R.id.rightCharacterListView).setVisibility(
						View.INVISIBLE);
				mJieBieListData = mjiebieData;
				mHandler.sendEmptyMessage(FINISHED_JIEBIE);
			}
		}.start();
	}

	/**
	 * 通讯录册子数据
	 */
	private void addtongxunluListData() {
		ll.setVisibility(View.GONE);
		findViewById(R.id.rightCharacterListView).setVisibility(View.INVISIBLE);
		new Thread() {
			public void run() {
				mHandler.sendEmptyMessage(FINISHED_TXL);
			}
		}.start();
	}

	/**
	 * 添加右侧快速定位条
	 */
	private void addRightView() {
		mRightCharacterListView = (RightCharacterListView) findViewById(R.id.rightCharacterListView);
		mRightCharacterListView.setB(b);
		mRightCharacterListView
				.setOnTouchingLetterChangedListener(new LetterListViewListener());
	}

	/**
	 * 移除右侧快速定位条 占无使用
	 */
	private void removeRightView() {
		mRightCharacterListView = (RightCharacterListView) findViewById(R.id.rightCharacterListView);
		mRightCharacterListView.setB(c);
	}

	/**
	 * 初始化首字母悬浮提示框
	 */
	private void addOverLay() {
		mTxtOverLay = (TextView) LayoutInflater.from(this).inflate(
				R.layout.popup_char, null);
		mTxtOverLay.setVisibility(View.INVISIBLE);
		WindowManager.LayoutParams lp = new WindowManager.LayoutParams(
				LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT,
				WindowManager.LayoutParams.TYPE_APPLICATION,
				WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE
						| WindowManager.LayoutParams.FLAG_NOT_TOUCHABLE,
				PixelFormat.TRANSLUCENT);
		mWindowManager = (WindowManager) getSystemService(Context.WINDOW_SERVICE);
		mWindowManager.addView(mTxtOverLay, lp);
	}

	@Override
	protected void onDestroy() {
		super.onDestroy();
		mDbHelper.closeDB();
		if (mTxtOverLay != null) {
			mTxtOverLay.setVisibility(View.GONE);
			mWindowManager.removeView(mTxtOverLay);
		}
	}

	@SuppressLint("HandlerLeak")
	private Handler mHandler = new Handler() {

		public void handleMessage(android.os.Message msg) {
			mSearchEditText.setFocusable(true);
			mSearchEditText.setFocusableInTouchMode(true);
			switch (msg.what) {
			case FINISHED_JIEBIE: // 界别
				findViewById(R.id.progressBar1).setVisibility(View.GONE);
				if (mJieBieListData != null) {
					mGroupArrays = ListTest.getJieBie(mJieBieListData);
					mData = ListTest.getLists(mGroupArrays, mJieBieListData);
					jiebieBaseAdapter = new ContactsJiebieBaseAdapter(
							ContactsActivity.this, mData, mGroupArrays,
							mContactsListView2);
					mContactsListView2.setAdapter(jiebieBaseAdapter);
				}
				break;
			case FINISHED_TXL: // 专委会
				findViewById(R.id.progressBar1).setVisibility(View.GONE);
				if (contactsBean3s != null) {
					mGroupArrays = ListTest.getZWH(numverShort);
					mData = ListTest.getLists1(mGroupArrays, numverShort);
					weiyuanBaseAdapter = new ContactsweiyuanhuiBaseAdapter(
							ContactsActivity.this, mData, mGroupArrays,
							mContactsListView2);
					mContactsListView2.setAdapter(weiyuanBaseAdapter);
				}
				break;

			default:
				break;
			}
		};
	};

	/**
	 * 获取�?��数据
	 * 
	 * 
	 */
	private class GetBanBenTask extends AsyncTask<Void, Void, Integer> {
		private boolean flag = false;

		@Override
		protected void onPreExecute() {
			mProgressBar.setVisibility(View.VISIBLE);
			super.onPreExecute();
		}

		@Override
		protected Integer doInBackground(Void... params) {
			try {

				ContactsBanBen contactsBanBen = DataUtil.getContactsBanben();
				newBanben = contactsBanBen.getVersion();
			} catch (Exception e) {
				e.printStackTrace();
				newBanben = "0";
				flag = false;
			}
			return 1;
		}

		@Override
		protected void onPostExecute(Integer result) {
			super.onPostExecute(result);
			if (flag == true) {
				if (oldBanben == null) {
					new GetNewDataTask().execute();
				} else {
					if (Integer.valueOf(newBanben) > Integer.valueOf(oldBanben)) {
						new GetNewDataTask().execute();
					} else {
						new GetDBDataTask().execute();
					}
				}
			} else {
				new GetDBDataTask().execute();
			}
		}
	}

	private class GetDBDataTask extends AsyncTask<Void, Void, Integer> {
		private boolean flag = false;

		@Override
		protected void onPreExecute() {
			super.onPreExecute();
		}

		@Override
		protected Integer doInBackground(Void... params) {
			try {
				contactsBeans = dbHelper.getContactsList();
				numverShort = new ArrayList<ContactsBean>();
				contactsBean3s = new ContactsBean[500];

				for (int i = 0; i < contactsBeans.size(); i++) {
					contactsBean3s[Integer.parseInt(contactsBeans.get(i)
							.getWybh())] = contactsBeans.get(i);
				}

				for (int i = 0; i < contactsBean3s.length; i++) {
					if (contactsBean3s[i] != null) {
						numverShort.add(contactsBean3s[i]);
					}
				}
				flag = true;
			} catch (Exception e) {
				e.printStackTrace();
				flag = false;
			}
			return 1;
		}

		@Override
		protected void onPostExecute(Integer result) {

			super.onPostExecute(result);
			if (flag == true) {
				if (contactsBeans != null && contactsBeans.size() > 0) {

					mnametData = contactsBeans;
					for (int i = 0; i < contactsBeans.size(); i++) {
						mjiebieData.add(contactsBeans.get(i));
					}
					findViewById(R.id.progressBar1).setVisibility(View.GONE);
					Comparent comparent = new Comparent();
					Collections.sort(mnametData, comparent);
					contactsBaseAdapter = new ContactsBaseAdapter(
							ContactsActivity.this, mnametData,
							mContactsListView);
					mContactsListView.setAdapter(contactsBaseAdapter);
					contactsBaseAdapter.notifyDataSetChanged();
					addRightView();
					mAllCListData = mnametData;
					mRightCharacterListView.setVisibility(View.VISIBLE);
				} else {
					new GetNewDataTask().execute();
				}
			} else {
				new GetNewDataTask().execute();
			}
			mProgressBar.setVisibility(View.GONE);
		}
	}

	/**
	 * 获取�?��数据
	 * 
	 * 
	 */
	private ArrayList<ContactsBean> numverShort;
	private ContactsBean[] contactsBean3s;
	private LinearLayout ll;

	private class GetNewDataTask extends AsyncTask<Void, Void, Integer> {
		private boolean flag = false;
		private ArrayList<ContactsBean> contactsBeans2;

		@Override
		protected void onPreExecute() {
			super.onPreExecute();
		}

		@Override
		protected Integer doInBackground(Void... params) {
			try {
				contactsBeans2 = JsonUtil.getData(StringUtil.getStrForJson(
						"txl.js", ContactsActivity.this));
//				contactsBeans2 = DataUtil.getContactsBeans();
				numverShort = new ArrayList<ContactsBean>();
				contactsBean3s = new ContactsBean[500];

				for (int i = 0; i < contactsBeans2.size(); i++) {
					contactsBean3s[Integer.parseInt(contactsBeans2.get(i)
							.getWybh())] = contactsBeans2.get(i);
				}

				for (int i = 0; i < contactsBean3s.length; i++) {
					if (contactsBean3s[i] != null) {
						numverShort.add(contactsBean3s[i]);
					}
				}
				for (@SuppressWarnings("rawtypes")
				Iterator iterator = contactsBeans2.iterator(); iterator
						.hasNext();) {
					ContactsBean contactsBean = (ContactsBean) iterator.next();
					dbHelper.setContacts(contactsBean);
				}
				flag = true;
			} catch (Exception e) {
				e.printStackTrace();
				flag = false;
			}
			return 1;
		}

		@Override
		protected void onPostExecute(Integer result) {
			super.onPostExecute(result);
			if (flag == true) {
				mnametData = contactsBeans2;
				for (int i = 0; i < contactsBeans2.size(); i++) {
					mjiebieData.add(contactsBeans2.get(i));
				}

				findViewById(R.id.progressBar1).setVisibility(View.GONE);
				Comparent comparent = new Comparent();
				Collections.sort(mnametData, comparent);
				Toast.makeText(ContactsActivity.this, "数据数据加载成功!",
						Toast.LENGTH_SHORT).show();
				contactsBaseAdapter = new ContactsBaseAdapter(
						ContactsActivity.this, mnametData, mContactsListView);
				mContactsListView.setAdapter(contactsBaseAdapter);
				contactsBaseAdapter.notifyDataSetChanged();
				dbHelper.setContactsBanBen(newBanben);
				mAllCListData = mnametData;
				try {
					mRightCharacterListView.setVisibility(View.VISIBLE);
				} catch (Exception e) {
					e.printStackTrace();
				}
				addRightView();
			} else {
				Toast.makeText(ContactsActivity.this, "数据加载失败!",
						Toast.LENGTH_SHORT).show();
			}
			mProgressBar.setVisibility(View.GONE);
		}
	}

	public ArrayList<ContactsBean> NumverShort(ArrayList<ContactsBean> ve) {
		ArrayList<ContactsBean> ve1 = new ArrayList<ContactsBean>();
		for (int i = 0; i < ve.size(); i++) {
			for (int x = 0; x < ve.size() - i - 1; x++) {
				if (Integer.parseInt(ve.get(x).getWybh()) < Integer.parseInt(ve
						.get(x + 1).getWybh())) {
					String temp = ve.get(x).getWybh();
					ve.get(x).setWybh(ve.get(x + 1).getWybh());
					ve.get(x + 1).setWybh(temp);
					ve1.add(ve.get(x));

				}
			}
		}
		return ve;
	}

	public class LetterListViewListener implements
			OnTouchingLetterChangedListener {
		@Override
		public void onTouchingLetterChanged(String s) {
			int num = 0;
			if (mnametData != null && mnametData.size() != 0) {
				for (int i = 0; i < mnametData.size(); i++) {
					if ("a".equals(s)) {
						num = 0;
					} else {
						ContactsBean bean = mnametData.get(i);
						if (bean != null) {
							String name = bean.getName();
							if (name != null && !name.trim().equals("")) {
								if (character2ASCII(CnToSpellUtils
										.getFullSpell(name.trim()).substring(0,
												1)) < (character2ASCII(s) + 32)) {
									num += 1;
								}

							}
						}
					}
				}
				mTxtOverLay.setText(s);
				mTxtOverLay.setVisibility(View.VISIBLE);
				mContactsListView.setSelectionFromTop(num, 0);
			}
		}

	}

	/**
	 * 把单个英文字母或者字符串转换成数字ASCII码
	 * 
	 * @param input
	 * @return
	 */
	public static int character2ASCII(String input) {
		char[] temp = input.toCharArray();
		StringBuilder builder = new StringBuilder();
		for (char each : temp) {
			builder.append((int) each);
		}
		String result = builder.toString();
		return Integer.parseInt(result);
	}

	@Override
	public void onClick(View v) {
		mSearchEditText.setFocusable(false);
		mSearchEditText.getText().clear();
		mCancelBtn.setVisibility(View.GONE);
		closeInputMethod();
		switch (v.getId()) {
		case R.id.btn_goback:
			finish();
			break;
		case R.id.btn_ref:
			new GetNewDataTask().execute();
			break;
		case R.id.btn_change:
			if (isJiebie) {
				// 界别
				searchType = "jiebie";
				mChangeBtn.setBackgroundResource(R.drawable.pinseq_down);
				findViewById(R.id.progressBar1).setVisibility(View.VISIBLE);
				mContactsListView.setVisibility(View.GONE);
				mContactsListView2.setVisibility(View.VISIBLE);
				addJiebieListData();
				isJiebie = false;
				isname = true;
				iszhuanweihui = false;
			} else if (isname) {
				// 全部
				searchType = "name";
				mChangeBtn.setBackgroundResource(R.drawable.weiyuan_down);
				findViewById(R.id.progressBar1).setVisibility(View.VISIBLE);
				mRightCharacterListView.setVisibility(View.GONE);
				mContactsListView2.setVisibility(View.GONE);
				mContactsListView.setVisibility(View.VISIBLE);
				addNameListData();
				isJiebie = false;
				isname = false;
				iszhuanweihui = true;
			} else if (iszhuanweihui) {
				// 专委会
				searchType = "jiebie";
				mChangeBtn.setBackgroundResource(R.drawable.jieseq_down);
				findViewById(R.id.progressBar1).setVisibility(View.VISIBLE);
				mContactsListView.setVisibility(View.GONE);
				mContactsListView2.setVisibility(View.VISIBLE);
				addtongxunluListData();
				isJiebie = true;
				isname = false;
				iszhuanweihui = false;
			}
			break;

		default:
			break;
		}
	}

	/**
	 * 关闭输入法
	 */
	private void closeInputMethod() {
		InputMethodManager inputMethodManager = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
		boolean isopen = inputMethodManager.isActive();
		if (isopen) {
			if (ContactsActivity.this.getCurrentFocus() != null) {
				inputMethodManager.hideSoftInputFromWindow(
						ContactsActivity.this.getCurrentFocus()
								.getWindowToken(),
						InputMethodManager.HIDE_NOT_ALWAYS);
			}
		}
	}

	class onClickListener implements OnClickListener {

		@Override
		public void onClick(View v) {
			mPopupWindow.dismiss();
			switch (v.getId()) {
			case R.id.call_layout:
				call(number);
				break;
			case R.id.message_layout:
				sms(number);
				break;
			default:
				break;
			}
		}

	}

	@Override
	public void onItemClick(AdapterView<?> parent, View v, int position,
			long arg3) {
		if (chat) {
			if (iszhuanweihui) {
				bean = mnametData.get(position);
			} else if (isjiebie) {
				bean = mjiebieData.get(position);
			} else if (isname) {
				bean = numverShort.get(position);
			}

			Intent chatIntent = new Intent(this, ChatActivity.class);
			chatIntent.putExtra("type", "0");
			chatIntent.putExtra("id", bean.getId());
			chatIntent.putExtra("ismessage", "2");
			Bundle bundle = new Bundle();
			bundle.putSerializable("chatContacts", bean);
			chatIntent.putExtras(bundle);
			startActivity(chatIntent);

		} else {
			if (iszhuanweihui) {
				bean = mnametData.get(position);
			} else if (isjiebie) {
				bean = mjiebieData.get(position);
			} else if (isname) {
				bean = numverShort.get(position);
			}

			Intent intent = new Intent(this, ContactsDetailActivity.class);
			Bundle bundle = new Bundle();
			bundle.putSerializable("contacts", bean);
			intent.putExtras(bundle);
			startActivity(intent);
		}

	}

	@Override
	public boolean onChildClick(ExpandableListView parent, View v,
			int groupPosition, int childPosition, long id) {
		if (isJiebie) {
			bean = weiyuanBaseAdapter.getChild(groupPosition, childPosition);
		} else if (isname) {
			bean = jiebieBaseAdapter.getChild(groupPosition, childPosition);
		}
		if (chat) {

			Intent chatIntent = new Intent(this, ChatActivity.class);
			chatIntent.putExtra("type", "0");
			chatIntent.putExtra("id", bean.getId());
			chatIntent.putExtra("ismessage", "2");
			Bundle bundle = new Bundle();
			bundle.putSerializable("chatContacts", bean);
			chatIntent.putExtras(bundle);
			startActivity(chatIntent);

		} else {
			Intent intent = new Intent(this, ContactsDetailActivity.class);
			Bundle bundle = new Bundle();
			bundle.putSerializable("contacts", bean);
			intent.putExtras(bundle);
			startActivity(intent);
		}
		return true;
	}

	@Override
	public boolean onGroupClick(ExpandableListView parent, View v,
			int groupPosition, long id) {
		if (expandFlag == -1) {
			// 展开被选的group
			mContactsListView2.expandGroup(groupPosition);
			// 设置被选中的group置于顶端
			mContactsListView2.setSelectedGroup(groupPosition);
			expandFlag = groupPosition;
		} else if (expandFlag == groupPosition) {
			mContactsListView2.collapseGroup(expandFlag);
			expandFlag = -1;
		} else {
			mContactsListView2.collapseGroup(expandFlag);
			// 展开被选的group
			mContactsListView2.expandGroup(groupPosition);
			// 设置被选中的group置于顶端
			mContactsListView2.setSelectedGroup(groupPosition);
			expandFlag = groupPosition;
		}
		return true;
	}

	/**
	 * @param ucall
	 *            拨打电话
	 */
	@SuppressLint("ShowToast")
	private void call(String ucall) {
		if (ucall != null && !ucall.trim().equals("")
				&& !ucall.trim().equals("null")) {
			try {
				Intent intent = new Intent(Intent.ACTION_CALL, Uri.parse("tel:"
						+ ucall));
				startActivity(intent);
			} catch (Exception e) {
				Toast.makeText(ContactsActivity.this, "未能拨打电话...", 500).show();
			}
		} else {
			Toast.makeText(ContactsActivity.this, "未能拨打电话...", 500).show();
		}
	}

	/**
	 * @param ucall
	 *            手机号码 发送短信
	 */
	@SuppressLint("ShowToast")
	private void sms(String ucall) {
		try {
			if (ucall != null && !ucall.trim().equals("")
					&& !ucall.trim().equals("null")) {
				Intent intent = new Intent(Intent.ACTION_SENDTO,
						Uri.parse("smsto:" + ucall));
				startActivity(intent);
			} else {
				Toast.makeText(ContactsActivity.this, "未能发送短信...", 500).show();
			}
		} catch (Exception e) {
			e.printStackTrace();
			Toast.makeText(ContactsActivity.this, "未能发送短信...", 500).show();
		}
	}

}
